
import { RankData, RankItemData } from './opendata';
import RankItem from './rankitem';

const {ccclass, property} = cc._decorator;

@ccclass
export default class Rank extends cc.Component {

    @property([cc.Sprite])
    avatarTop: cc.Sprite[] = [];

    @property(cc.Prefab)
    rankItem: cc.Prefab = null;

    @property(cc.ScrollView)
    scrollView: cc.ScrollView = null;

    @property(cc.Node)
    selfRank: cc.Node = null;
    @property(cc.Label)
    selfIndex: cc.Label = null;
    @property(cc.Label)
    selfName: cc.Label = null;
    @property(cc.Label)
    selfScore: cc.Label = null;
    @property(cc.Sprite)
    selfAvatar: cc.Sprite = null;

    private _rankData: RankData = null;
    private _curShow: string = null;

    start () {
        this.node.on('resetUserInfo', this.resetUserInfo, this);
        this.node.on('showRankAll', function(event: cc.Event.EventCustom) {
            this._curShow = 'all';
            // 显示前10名
            this.showRank(event.detail, 10);
        }, this);
        this.node.on('showRankGroup', function(event: cc.Event.EventCustom) {
            this._curShow = 'group';
            this.showRank(event.detail);
        }, this);
        this.node.on('showRankFriend', function(event: cc.Event.EventCustom) {
            this._curShow = 'friend';
            this.showRank(event.detail);
        }, this);
        this.node.on('showRankFriendTop', function(event: cc.Event.EventCustom) {
            this._curShow = 'friendTop';
            this.showTop(event.detail);
        }, this);
        this.node.on('resetFriendRank', function(event: cc.Event.EventCustom) {
            if (this._curShow === 'friendTop') {
                this.showTop(event.detail);
            } else if (this._curShow === 'friend') {
                this.showRank(event.detail);
            }
        }, this);
        this.node.on('showRankList', this.showRankList, this);
        this.node.on('hideRankList', this.hideRankList, this);
        this.node.on('disableRankList', this.disableRankList, this);
    }

    resetUserInfo () {
        let selfIndex = this._rankData && this._rankData.selfIndex;
        let selfData:RankItemData = this._rankData && this._rankData.items[selfIndex];
        if (selfData) {
            this.selfRank.active = true;
            let index = (typeof selfIndex === 'number') ? selfIndex + 1 : selfIndex;
            this.selfIndex.string = index.toString();
            this.selfName.string = selfData.nickName;
            this.selfScore.string = selfData.score.toString();
            RankItem.prototype.setAvatar(this.selfAvatar, selfData.avatarUrl);
        } else {
            this.selfRank.active = false;
        }
    }

    showRank (rankData, limit) {
        this.hideTop();
        limit = limit || rankData.items.length;
        if (limit < rankData.items.length) {
            let selfIndex = rankData.selfIndex;
            rankData["selfIndex"] = '未上榜';
            rankData.items[rankData["selfIndex"]] = rankData.items[selfIndex];
        }
        this._rankData = rankData;
        this.resetUserInfo();
        this.scrollView.node.active = true;
        this.scrollView.content.destroyAllChildren();
        rankData.items.forEach((e, ix)=>{
            if (ix < limit) {
                let node = cc.instantiate(this.rankItem);
                node.getComponent(RankItem).init(ix, e);
                this.scrollView.content.addChild(node);
            }
        });
    }

    showTop(rankData: RankData) {
        // this.scrollView.node.active = false;
        this.avatarTop.forEach((sprite, ix)=>{
            if (ix < rankData.items.length) {
                sprite.node.parent.active = true;
                RankItem.prototype.setAvatar(sprite, rankData.items[ix].avatarUrl);
            } else {
                sprite.node.parent.active = false;
            }
        });
    }

    hideTop() {
        this.avatarTop.forEach((sprite)=>{
            sprite.node.parent.active = false;
        });
    }

    showRankList() {
        let node = this.scrollView.node;
        node.x -= 500;
        node.active = true;
        node.runAction(cc.moveBy(0.5, cc.v2(500, 0)));
    }

    hideRankList() {
        let node = this.scrollView.node;
        node.runAction(cc.sequence(
            cc.moveBy(0.5, cc.v2(-500, 0)),
            cc.callFunc(()=> {
                node.x += 500;
                node.active = false;
            })
        ));
    }

    disableRankList() {
        this.scrollView.node.active = false;
    }
}
